package download.itydl.com.sinlethread.mutilthread.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

    private static DatabaseHelper sDatabaseHelper;

    /**
     * 创建保存线程信息表
     */
    private static final String SQL_CREATE =
            "create table thread_info(" +
                    "_id integer primary key autoincrement," +
                    "thread_id integer," +
                    "url text," +
                    "start long," +
                    "end long," +
                    "finished long)";

    private static final String SQL_DROP = "drop table if exists thread_info";

    private static final String DB_NAME = "download.db";

    private static final int VERSION = 1;

    private DatabaseHelper(Context context) {
        super(context, DB_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(SQL_DROP);
        db.execSQL(SQL_CREATE);
    }

    public static DatabaseHelper getInstance(Context context){
        if(sDatabaseHelper == null){
            synchronized (DatabaseHelper.class){
                if(sDatabaseHelper == null){
                    sDatabaseHelper = new DatabaseHelper(context);
                }
            }
        }
        return sDatabaseHelper;
    }
}
